package com.bh.web.controller.shipment;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.shipment.domain.JcShipment;
import com.bh.shipment.service.IJcShipmentService;
import com.bh.shipment.vo.JcShipmentAbnormalVO;
import com.bh.shipment.vo.JcShipmentListVo;
import com.bh.shipment.vo.JcShipmentSysVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 新运单管理Controller
 *
 * @author slx
 * @date 2023-12-23
 */
@RestController
@RequestMapping("/shipment/shipment")
public class JcShipmentController extends BaseController {
    @Autowired
    private IJcShipmentService jcShipmentService;

    /**
     * 查询新运单管理列表
     */
    @ApiOperation("查询新运单管理列表")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcShipmentSysVo shipment) {
        shipment.setJcOrganizationId(getOrgId());
        PageInfo<JcShipmentListVo> res = jcShipmentService.selectPageJcShipmentList(shipment);
        return getDataTable(res.getList(), res.getTotal());
    }

    /**
     * 导出新运单管理列表
     */
    @PreAuthorize("@ss.hasPermi('shipment:shipment:export')")
    @Log(title = "新运单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcShipmentSysVo jcShipment) {
        List<JcShipmentListVo> list = jcShipmentService.selectJcShipmentList(jcShipment);
        ExcelUtil<JcShipmentListVo> util = new ExcelUtil<>(JcShipmentListVo.class);
        util.exportExcel(response, list, "新运单管理数据");
    }

    /**
     * 获取新运单管理详细信息
     */
    @ApiOperation("获取新运单管理详细信息")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcShipmentService.selectJcShipmentById(id));
    }

    /**
     * 新增新运单管理
     */
    @ApiOperation("新增新运单管理")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:add')")
    @Log(title = "新运单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcShipment jcShipment) {
        if (jcShipment.getTime() == null) {
            jcShipment.setTime(new Date());
        }
        if (jcShipment.getJcVehicleId() == null) {
            return error("车型不能为空");
        }
        return toAjax(jcShipmentService.insertOrUpdateJcShipment(jcShipment));
    }

    /**
     * 修改新运单管理
     */
    @ApiOperation("修改新运单管理")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:edit')")
    @Log(title = "新运单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcShipment jcShipment) {
        if (jcShipment.getTime() == null) {
            jcShipment.setTime(new Date());
        }
        if (jcShipment.getJcVehicleId() == null) {
            return error("车型不能为空");
        }
        if (jcShipment.getStatus() > 1 || jcShipment.getStatus() < 0) {
            return error("运单不能保存，请联系管理员");
        }
        return toAjax(jcShipmentService.insertOrUpdateJcShipment(jcShipment));
    }


    /**
     * 修改新运单管理
     */
    @ApiOperation("修改新运单管理")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:edit')")
    @Log(title = "新运单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/saveAbnormalShipment")
    public AjaxResult saveAbnormalShipment(@RequestBody @Validated JcShipmentAbnormalVO abnormalVO) {
        JcShipment jcShipment = abnormalVO.getJcShipment();
        if (jcShipment.getTime() == null) {
            jcShipment.setTime(new Date());
        }
        if (jcShipment.getJcVehicleId() == null) {
            return error("车型不能为空");
        }
        return toAjax(jcShipmentService.saveAbnormalShipment(abnormalVO));
    }

    /**
     * 删除新运单管理
     */
    @ApiOperation("删除新运单管理")
    @PreAuthorize("@ss.hasPermi('shipment:shipment:remove')")
    @Log(title = "新运单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jcShipmentService.deleteJcShipmentByIds(ids));
    }


    /**
     * 获取运单追踪LIST
     */
    @ApiOperation("获取运单追踪LIST")
    @GetMapping("/trackShipmentList")
    public TableDataInfo trackShipmentList(JcShipmentSysVo arrive) {
        startPage();
        arrive.setJcOrganizationId(getOrgId());
        List<JcShipmentListVo> list = jcShipmentService.selectTrackShipmentList(arrive);
        return getDataTable(list);
    }

    /**
     * 获取 到站记录
     */
    @ApiOperation("获取到站记录Info")
    @GetMapping("/getDZShipmentInfo")
    public TableDataInfo getDZShipmentInfo(JcShipmentSysVo arrive) {
        startPage();
        arrive.setJcOrganizationId(getOrgId());
        List<JcShipmentListVo> list = jcShipmentService.getDZShipmentInfo(arrive);
        return getDataTable(list);
    }


    /**
     * 获取 到站记录
     */
    @PreAuthorize("@ss.hasPermi('shipment:shipment:dzlist')")
    @ApiOperation("获取到站记录Index")
    @GetMapping("/getDZShipmentList")
    public TableDataInfo getDZShipmentList(JcShipmentSysVo arrive) {
        startPage();
        arrive.setJcOrganizationId(getOrgId());
        List<JcShipmentListVo> list = jcShipmentService.getDZShipmentList(arrive);
        return getDataTable(list);
    }


    /**
     * 专门为别的机构查看到站车辆
     */
    @ApiOperation("获取运单轨迹")
    @GetMapping("/getRVShipmentList")
    public TableDataInfo getRVShipmentList(JcShipmentSysVo arrive) {
        startPage();
        arrive.setJcOrganizationId(getOrgId());
        List<JcShipmentListVo> list = jcShipmentService.getRVShipmentList(arrive);
        return getDataTable(list);
    }

    /**
     * 打印车辆运单
     */
    @PreAuthorize("@ss.hasPermi('shipment:shipment:print')")
    @ApiOperation("打印车辆运单")
    @PostMapping("/printShipment/{ids}")
    public void printShipment(HttpServletResponse response, @PathVariable Long[] ids) {
        jcShipmentService.printShipment(response, ids);
    }


    /**
     * 打印车辆运单
     */
    @PreAuthorize("@ss.hasPermi('shipment:shipment:print')")
    @ApiOperation("打印车辆运单")
    @GetMapping("/printShipmentData/{ids}")
    public AjaxResult getPrintShipmentData(@PathVariable Long[] ids) {
        return AjaxResult.success(jcShipmentService.printShipmentData(ids));
    }

    /**
     * 直接打印签收单
     */
    @Log(title = "直接打印签收单", businessType = BusinessType.EXPORT)
    @PostMapping("/printSignPdf/{ids}")
    public void printSignPdf(HttpServletResponse response, @PathVariable Long[] ids) {
        jcShipmentService.printSignPdf(response, ids);
    }


    /**
     * 直接打印签收单
     */
    @Log(title = "直接打印签收单", businessType = BusinessType.EXPORT)
    @GetMapping("/printSignBase64Pdf/{ids}")
    public AjaxResult printSignBase64Pdf(@PathVariable Long[] ids) {
        return AjaxResult.success("获取base64pdf成功!", jcShipmentService.printSignBase64Pdf(ids));
    }
}
